package com.jindi.appuser;


import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Properties;
//@EnableEurekaClient
@EnableTransactionManagement
@ServletComponentScan
@MapperScan("com.jindi.**.dao")
@SpringBootApplication
@EnableScheduling
public class YysApplication {
    public static void main(String[] args) {
        SpringApplication.run(YysApplication.class, args);
        System.out.println("" +
                "██████╗ ██╗  ██╗ ██╗ ██████╗     ██╗  ██╗██╗██╗     ██╗         ███████╗ █████╗ ████████╗██████╗ ████████╗\n" +
                "██╔══██╗██║ ██╔╝███║██╔═████╗    ██║ ██╔╝██║██║     ██║         ██╔════╝██╔══██╗╚══██╔══╝██╔══██╗╚══██╔══╝\n" +
                "██████╔╝█████╔╝ ╚██║██║██╔██║    █████╔╝ ██║██║     ██║         ███████╗███████║   ██║   ██████╔╝   ██║\n" +
                "██╔═══╝ ██╔═██╗  ██║████╔╝██║    ██╔═██╗ ██║██║     ██║         ╚════██║██╔══██║   ██║   ██╔══██╗   ██║\n" +
                "██║     ██║  ██╗ ██║╚██████╔╝    ██║  ██╗██║███████╗███████╗    ███████║██║  ██║   ██║   ██║  ██║   ██║\n" +
                "╚═╝     ╚═╝  ╚═╝ ╚═╝ ╚═════╝     ╚═╝  ╚═╝╚═╝╚══════╝╚══════╝    ╚══════╝╚═╝  ╚═╝   ╚═╝   ╚═╝  ╚═╝   ╚═╝\n");
    }

    //配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        //Access-Control-Allow-Origin
        properties.setProperty("dialect", "mysql");    //配置mysql数据库的方言
        pageHelper.setProperties(properties);
        return pageHelper;
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    /**
     * 跨域过滤器
     *
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }
}
